package com.amit;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CookieDemoServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
			{
		
		String em=request.getParameter("email");
		Cookie ck[]=request.getCookies();
		if(ck!=null)
		{
			if(ck.length!=0)
			{
				for(Cookie c:ck)
				{
					String cn=c.getName();
					if(cn.equals("JSESSIONID"))
					{
						System.out.println("U are old user");
					}
					String cv=c.getValue();
					String d=c.getDomain();
					System.out.println(cn+"\t"+cv+"\t"+d);
				}
				
			}
			else
			{
				System.out.println("soory, Noo cookies Found");
			}
		}
			else
			{
			System.out.println("Sorry ,Noo cookies Found");	
			}
		
		HttpSession session=request.getSession();
		
		boolean b=session.isNew();
		System.out.println(b);
		if(b)
		{
			System.out.println("You are new user");
		}
		
		else
		{
			System.out.println("u are old user");
		}
		
		Cookie c1=new Cookie("Email", em);
		response.addCookie(c1);
		Cookie c2=new Cookie("Phone", "1233");
		response.addCookie(c2);
		
		RequestDispatcher rd=request.getRequestDispatcher("cookiedemo.html");
		rd.forward(request, response);
		
		}
		
	}

